package dk.dmaa0912.boardgame;

import java.util.ArrayList;




import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import android.app.Activity;

//
public class Mail extends Activity {

	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		Intent intent = getIntent();
		
		ArrayList<String> mails = intent.getStringArrayListExtra("Mail");
		ArrayList<String> words = intent.getStringArrayListExtra("Words");
		
		String[] mailtaks = mails.toArray(new String[mails.size()]);
		String[] wordtasks = words.toArray(new String[words.size()]);
		
		sendEmail(mailtaks, wordtasks);
		
		
	}
	
	
	protected void sendEmail(String[]mails, String[] words) {
		Log.i("Send email", "");
		
				
		String[] TO = mails;

		StringBuilder sb = new StringBuilder();
		for (String s : words)
		{
			sb.append(s);
			sb.append("\n");
		}
		
		Intent emailIntent = new Intent(Intent.ACTION_SEND);
		emailIntent.setData(Uri.parse("mailto:"));
		emailIntent.setType("text/plain");
		
		emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
		
	    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Game Words");
	    emailIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());
	    
	    try {
	    	startActivityForResult(Intent.createChooser(emailIntent, "Send Email..."), 0);
	    	setResult(RESULT_OK);
	    
			
		} 
	    catch (android.content.ActivityNotFoundException ex) {
			Toast.makeText(this, "There is no email client installed", Toast.LENGTH_SHORT).show();
			setResult(RESULT_CANCELED);
		}
	    
	   
	
	    
	}
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode == RESULT_OK);
		{
			Toast.makeText(this, "Succes!", Toast.LENGTH_SHORT).show();
	        finish();
	        Intent exit = new Intent(this, BoardMainActivity.class);
	        exit.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	        exit.putExtra("EXIT", true);
	        
	        startActivity(exit);

		}

	}
	
	
}
